﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 蓄力一击
/// </summary>
public class ShotAct : IAction
{
    private ShotRender sr;
    public Vector3 Tagert { get { return sr.tagert; } }//返回瞄准的位置
    public override void Init()
    {
        GameObject go= trans.Find(ConstField.SHOT_AIM).gameObject;
        go.SetActive(true);
        var lr = go.GetComponent<LineRenderer>();
        var shotEffect = GameObject.Find(ConstField.SHOT_EFFECT).transform;//fix me  此处根据名字来找到特效物体
        sr = new ShotRender(shotEffect, lr,mva.shotAttr.maxAngle,mva.shotAttr.distance);
        if (shotEffect == null) Debug.LogError("can't find shot effet on " + trans.name);
    }

    public override void GetAction()
    {
        if (Input.GetMouseButton(0))
        {
            mva.IsHolding = true;
            mva.isFlying = false;
            //PosUpdata();
            sr.Updata(trans.position,Facade.GetMousePoint(),mva.LookAtLeft);

            //Debug.Log(GetMousePoint());
        }
        else
        {
            mva.IsHolding = false;
            sr.NotRender();
        }

    }
    /// <summary>
    /// 发射子弹
    /// </summary>
    public override void Act()
    {
        sr.ShotAct((int)EffectType.PowerOrbBlue, mva.shotAttr.startVelocity,
            mva.shotAttr.dueTime);
    }
    public ShotAct(PlayerController pc) : base(pc) { }
}

/// <summary>
/// 辅助线渲染
/// 矫正粒子位置
/// </summary>
class ShotRender
{
    public Transform effect;
    public LineRenderer line;
    //range=[0,90]
    public float maxAngle = 45;
    public Vector3 tagert=Vector3.zero;
    public float effectDistance = 0.4f;
    public ShotRender(Transform effect, LineRenderer line,float maxAngle,float distance)
    {
        this.effect = effect;
        this.line = line;
        this.maxAngle = maxAngle;
        effectDistance = distance;
    }

    public void Updata(Vector3 from ,Vector3 to, bool lookLeft)
    {
        Vector3 lerp = to - from;//差值
        //Debug.Log(Vector3.Angle(lerp,Vector3.up)) ;
        if (lerp == Vector3.zero) return;
        float angle = Vector3.Angle(lerp, Vector3.up);
        if (angle < (90 - maxAngle) || angle > (maxAngle + 90)) return;

        if ((lerp.x <= 0) != lookLeft)
        {
            tagert = from - lerp;
        }
        else
        {
            tagert = to;
        }

        line.SetPosition(1, tagert);
        line.SetPosition(0, from);
        line.enabled = true;

        Vector3 ePos = Vector3.MoveTowards(from, tagert, effectDistance);
        effect.position = ePos;
        effect.LookAt(tagert);
    }
    /// <summary>
    /// 发射类型为type的子弹
    /// </summary>
    /// <param name="type"></param>
    public void ShotAct(int type,float startVelocity,float dueTime)
    {
      //  IEffect e = Facade.Instance.Effects.Initiate<IEffect>(type, effect.position);
      //  e.Play(dueTime);

        IEffect[] es = IEffect.PlayWhere(type, effect.position, dueTime);

        Rigidbody rig = es[0].GetComponent<Rigidbody>();
        if (rig)
        {
            rig.velocity = startVelocity*(tagert-effect.position).normalized;
        }
    }

    public void NotRender()
    {
        line.enabled = false;
    }
}
